package com.kedou.sdk.params;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.kedou.sdk.utils.WL;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/27 0027.
 */

public abstract class RequestDataParams {

    private static final String TAG = RequestDataParams.class.getSimpleName();
    private static final TypeAdapter<String> STRING = new TypeAdapter<String>() {

        @Override
        public void write(JsonWriter jsonWriter, String value) throws IOException {
            if (TextUtils.isEmpty(value)) {
                jsonWriter.nullValue();
            } else {
                jsonWriter.value(value);
            }
        }

        @Override
        public String read(JsonReader jsonReader) throws IOException {
            if (jsonReader.peek() == JsonToken.NULL) {
                jsonReader.nextNull();
                return null;
            }

            String result = jsonReader.nextString();
            return TextUtils.isEmpty(result) ? null : result;
        }

    };

    public abstract String getServiceString();

    public final JSONObject toJsonObject() {
        Gson gson = new GsonBuilder().registerTypeAdapter(String.class, STRING).create();
        String jsonObject = gson.toJson(this);

        JSONObject json = null;
        try {
            json = new JSONObject(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        WL.d(TAG, "toJsonString : " + json.toString());
        return json;
    }

    @Override
    public String toString() {
        Gson gson = new GsonBuilder().registerTypeAdapter(String.class, STRING).create();
        String jsonObject = gson.toJson(this);
        Map<String, Object> fields = gson.fromJson(jsonObject, new TypeToken<Map<String, String>>() {
        }.getType());

        List<Map.Entry<String, Object>> entrys = new ArrayList<>(fields.entrySet());
        Collections.sort(entrys, new StringASCIIComparator());

        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, Object> entry : entrys) {
            if (entry != null) {
                Object object = entry.getValue();
                if (object != null && !TextUtils.isEmpty(object.toString())) {
                    builder.append(entry.getKey());
                    builder.append("=");
                    builder.append(entry.getValue());
                    builder.append("&");
                }
            }
        }
        String ad=builder.substring(0,builder.length()-1);
        WL.d(TAG, "toString : " + ad);
        return ad;
    }

    private static class StringASCIIComparator implements Comparator<Map.Entry<String, Object>>, Serializable {


        private static final long serialVersionUID = 1525697019388357094L;

        @Override
        public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
            return o1.getKey().compareTo(o2.getKey());
        }
    }
}
